package com.wangtao.firstapp.fragments.messagefragments;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.MessageBoardActivity;
import com.wangtao.firstapp.modles.MessageNotice;
import com.wangtao.firstapp.utils.HttpUtils;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 消息界面下通知页面
 * Created by lanouhn on 16/8/27.
 */
public class NoticeFragment extends Fragment implements View.OnClickListener {
    private CircleImageView civ_message_notice;
    private TextView tv_name_message_notice, tv_content_message_notice, tv_clicktoleavemessage;
    private MessageNotice messagenotice;
    private RelativeLayout notice_message;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.noticefragment, null);
        civ_message_notice = (CircleImageView) view.findViewById(R.id.civ_message_notice);
        tv_name_message_notice = (TextView) view.findViewById(R.id.tv_name_message_notice);
        tv_content_message_notice = (TextView) view.findViewById(R.id.tv_content_message_notice);
        tv_clicktoleavemessage = (TextView) view.findViewById(R.id.tv_clicktoleavemessage);
        notice_message = (RelativeLayout) view.findViewById(R.id.notice_message);
        getData();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        civ_message_notice.setOnClickListener(this);
//        tv_name_message_notice.setOnClickListener(this);
//        tv_content_message_notice.setOnClickListener(this);
//        tv_clicktoleavemessage.setOnClickListener(this);
        notice_message.setOnClickListener(this);
    }

    private void getData() {
        String url = "http://open4.bantangapp.com/users/message/list";
        String params = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=ppzhushuo&app_installtime=1471923362&app_versions=5.8.6&os_versions=5.1&screensize=1080&v=18&page=0&pagesize=20";
        try {
            HttpUtils.doPostAsyn(url, params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    messagenotice = gson.fromJson(result, MessageNotice.class);
                    if (messagenotice.getData().getList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    //Toast.makeText(getActivity(), "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Picasso.with(getActivity()).load(messagenotice.getData().getList().get(0).getUser().getAvatar()).config(Bitmap.Config.RGB_565).into(civ_message_notice);
                    tv_name_message_notice.setText(messagenotice.getData().getList().get(0).getUser().getNickname());
                    tv_content_message_notice.setText(messagenotice.getData().getList().get(0).getContent());
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), MessageBoardActivity.class);
        startActivity(intent);
    }
}
